/**
 * Created by yanwe on 2016/7/4.
 */
/**
 * Created by yanwe on 2016/6/26.
 */
(function(){
    'use strict';
    app.controller('TransferOrderListController',[
        '$scope','transferOrderService','i18nService','$state','$stateParams',function($scope, transferOrderService,i18nService,$state,$stateParams){
            $(".datetimepicker_").datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                language:"cn",
                weekStart: 1,
                todayBtn:  "linked",
                startView: 2,
                autoclose: true,
                todayHighlight: true,
                endDate : new Date()
            });
            $scope.filterOptions = {
                condition:{
                    goodsName: "",
                    startTime:"",
                    endTime:"",
                    label:""
                },
                useExternalFilter: true
            };
            console.log($scope.filterOptions);
            i18nService.setCurrentLang('zh-cn');
            $scope.totalServerItems = 0;
            console.log($stateParams.pageSize,$stateParams.currentPage);
            $scope.pagingOptions = {
                pageSizes: [250, 500, 1000],
                pageSize: 250,
                currentPage: 1
            };
            $scope.setPagingData = function (data, page, pageSize) {
                $scope.myData = data.data;
                $scope.totalServerItems = data.totalServerItems;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };
            $scope.getPagedDataAsync = function (pageSize, page, condition) {
                setTimeout(function () {
                    if (condition) {
                        transferOrderService.getOrderList(condition.goodsName,condition.startTime,
                            condition.endTime,condition.label,page).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    } else {
                        transferOrderService.getOrderList('','','','',page).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    }
                }, 100);
            };
            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            $scope.$watch('pagingOptions', function (newVal, oldVal) {
                if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.$watch('filterOptions', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.showMe = function ($this) {
                $state.go('app.order.transfer_requisition_detail',{orderId:$this.row.entity.orderId});
                //console.log($this.row);
            };
            $scope.gridOptions = {
                paginationPageSizes: [15, 20, 25],
                paginationPageSize: 15,
                enableColumnResizing: true,
                enableGridMenu: true,
                columnDefs: [
                    {name: 'goodsNames', pinnedLeft: true, displayName: '商品名称', width: 200},
                    {name: 'orderTime', displayName: '下单时间', width: 190},
                    {name: 'orderStatus', displayName: '订单状态', width: 100},
                    {name: 'stylistName', displayName: '搭配师', width: 100},
                    {name: 'belongToName', displayName: '所属店面', width: 100},
                    {name: 'count', displayName: '数量', width: 80},
                    {name: 'description', displayName: '描述', width: 200},
                    {
                        name: '详情', width: 110,
                        pinnedRight: true,
                        cellTemplate: '<button class="btn btn-mini btn-primary table-btn" ng-click="grid.appScope.showMe(this)">详情</button>'
                    }
                ],
                data: 'myData'
            };
            $scope.searchByFilter=function(){
                $scope.filterOptions.condition=$scope.condition;
                console.log($scope.filterOptions);
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
            };
        }
    ]);

    app.controller('TransferOrderDetailController',[
        '$scope','$stateParams','$state','transferOrderService',function($scope, $stateParams,$state,transferOrderService){
            $scope.orderId=$stateParams.orderId;
            transferOrderService.getOrderDetail($scope.orderId).then(function(success){
                $scope.orderDetail=success.data;
            });
            $scope.backToOrderList=function(){
                $state.go('app.order.transfer_requisition_list',{currentPage:$stateParams.currentPage,pageSize:$stateParams.pageSize});
            };
            $scope.createInvoice=function(){
                $state.go('app.order.deliver_goods_invoices_create',{orderId:$scope.orderId});
            }
        }
    ]);
})();
